﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WCS_Service.DomianService.Interface;
using WCS_Service.DomianService.Models;
using WCS_Service.Models;
using WCS_Utility.ConstString;

namespace WCS_Service.DomianService
{
    /// <summary>
    /// 访问RCS超时操作
    /// </summary>
    public class RcsOvertimeDomainService : IRcsOvertimeDomainService
    {
        public event Func<string, Task> OvertimeCreatTaskChange;
        public ConcurrentDictionary<string, WaitRcsResultModel> WaitResultQueue { get; set; } = new();


        /// <summary>
        /// Rcs访问超时检查
        /// </summary>
        /// <returns></returns>
        public async Task OverTimeCheck()
        {
            List<Task> addCheck = new();
            foreach (var item in WaitResultQueue)
            {
                if ((DateTime.Now - item.Value.addTime).TotalSeconds > 20)
                {
                    addCheck.Add(RemoveWaitQuere(item.Key));
                }
            }
            await Task.WhenAll(addCheck);
        }
        /// <summary>
        /// 添加等待队列
        /// </summary>
        /// <param name="key"></param>
        /// <param name="apiName"></param>
        /// <returns></returns>
        public async Task AddWaitQuere(string key, string apiName,object para)
        {
            if (key is null || apiName is null || para is null)
            {
                return;
            }
            await Task.Run(() =>
            {
                WaitRcsResultModel waitRcsResultModel = new WaitRcsResultModel();
                waitRcsResultModel.addTime = DateTime.Now;
                waitRcsResultModel.reqCode = key;
                waitRcsResultModel.apiName = apiName;
                waitRcsResultModel.para = para;
                WaitResultQueue.AddOrUpdate(key, waitRcsResultModel, (key, value) => value);
            });
        }
        /// <summary>
        /// RCS访问超时
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task<WaitRcsResultModel> RemoveWaitQuere(string key)
        {
            return await Task.Run(async () =>
            {
                if (WaitResultQueue.TryRemove(key, out WaitRcsResultModel value))
                {
                    switch (value.apiName)
                    {
                        case ConstStringData.CreatTask_WCSApi:
                            await CreatTaskOvertime(value);
                            break;
                        case ConstStringData.QueryAgvStatus_WCSApi:
                            break;
                        case ConstStringData.ContinueTask_WCSApi:
                            break;
                    }
                }
                return value;
            });
        }

        #region RCS访问超时处理
        private async Task CreatTaskOvertime(WaitRcsResultModel info)
        {
            await Task.Run(() => 
            {
                var data = info.para as CreatTaskModel;
                if (data is not null)
                {
                    OvertimeCreatTaskChange?.Invoke(data.agvCode);
                }
            });
        }
        #endregion
    }
}
